package net.tycmc.cty.ctyms.timejob.iems.thread;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.dao.BaseDAO;
import net.tycmc.bulb.common.datasource.ProxoolDataSource;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.ListUtil;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSDAO;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSTransDAO;

import com.tycmc.functions.util.StringUtil;


public class OilOpenRunner implements Runnable{
	private MyConnectDSDAO mcdsDAO = new MyConnectDSDAO();
	private MyConnectDSTransDAO mcdstDAO = new MyConnectDSTransDAO();
	private String VclIDS; 
	private String workTime;
	private String isbakTable;
	private static String proxool = "proxool.test";
	private BaseDAO baseDAO;
	private Map<String,Object> param;
	public OilOpenRunner(String vclIDS,String workTime,String isbakTable,Map<String,Object> mapParam){
		this.VclIDS = vclIDS;
		this.workTime = workTime;
		this.isbakTable = isbakTable;
		this.param = mapParam;
		if(null == this.baseDAO){
			this.baseDAO = new BaseDAO();
			ProxoolDataSource connGetter = new ProxoolDataSource();
			connGetter.setProxoolname(proxool);
			this.baseDAO.setConnGetter(connGetter);
		}
	}

	@Override
	public void run() {
		ListUtil listUtil = new ListUtil();
		Map<String,List<Map<String,Object>>> pgn = listUtil.groupBy("MsgP_Vcl_ID", this.queryPgnOfPedalPosition(param));
		Iterator<String> it = pgn.keySet().iterator();
		StringBuffer strSQL = new StringBuffer();
		String oneVcl;
		List<Map<String,Object>> onePgnList;
		while(it.hasNext()){
			oneVcl = it.next();
			onePgnList = pgn.get(oneVcl);
			strSQL.append(this.getUpdateOilOpenSQL(oneVcl,onePgnList));
		}
		if(StringUtil.isValid(strSQL.toString()) && strSQL.toString().length()>0){
//			System.out.println("油门开度数据更新SQL语句："+strSQL.toString());
			int num = this.mcdsDAO.execUpdate(strSQL.toString(), new Object[]{});
			if(this.isbakTable.equals("1")){
				MyConnectDSDAO connect = new MyConnectDSDAO(true);
				int isbak = connect.execUpdate(strSQL.toString(), new Object[]{});
				if(isbak<0){
					System.out.println("油门开度数据备份失败");
				}
			}
//			System.out.println("油门开度数据更新影响条数："+ num);
		}else{
//			System.out.println("油门开度数据更新SQL语句为空");
		}
	}
    /**
     * 处理一台车的油门开度信息
     **/
	private String getUpdateOilOpenSQL(String VehicleID,List<Map<String, Object>> srcList) {
		String sql = "";
		if(null!= srcList && !srcList.isEmpty()){
			int isZero = 0;
			int underTen = 0;
			int tenToTwe = 0;
			int tweToThi = 0;
			int thiToFor = 0;
			int forToFif = 0;
			int fifToSix = 0;
			int sixToSev = 0;
			int sevToEig = 0;
			int eigToNin = 0;
			int ninToHun = 0;
			double tempDou = 0;
			for(Map<String,Object> temp:srcList){
				if(StringUtil.isValid(temp.get("MsgPC_PGNValue"))){
					tempDou = Double.parseDouble(temp.get("MsgPC_PGNValue").toString());
					if(tempDou == 0){
						isZero++;
					}else if(tempDou>0 && tempDou<=10){
						underTen ++;
					}else if(tempDou>10 && tempDou<=20){
						tenToTwe ++;
					}else if(tempDou>20 && tempDou<=30){
						tweToThi ++;
					}else if(tempDou>30 && tempDou<=40){
						thiToFor ++;
					}else if(tempDou>40 && tempDou<=50){
						forToFif ++;
					}else if(tempDou>50 && tempDou<=60){
						fifToSix ++;
					}else if(tempDou>60 && tempDou<=70){
						sixToSev ++;
					}else if(tempDou>70 && tempDou<=80){
						sevToEig ++;
					}else if(tempDou>80 && tempDou<=90){
						eigToNin ++;
					}else if(tempDou>90 && tempDou<=100){
						ninToHun ++;
					}else{
						// <0 || >100 暂时不处理
					}
				}
			}
			sql = "update OilSwitchOpen_"+this.workTime.replaceAll("-", "")+" set OSO_ValidCounts = "+(isZero+underTen+tenToTwe+tweToThi+thiToFor+forToFif+fifToSix+sixToSev+sevToEig+eigToNin+ninToHun)+"," +
				  "OSO_Zero = "+isZero+",OSO_UnderTen = "+underTen+",OSO_TenToTwe = "+tenToTwe+",OSO_TweToThi = "+tweToThi+"," +
				  "OSO_ThiToFor = "+thiToFor+",OSO_ForToFif = "+forToFif+",OSO_FifToSix = "+fifToSix+"," +
			      "OSO_SixToSev = "+sixToSev+",OSS_SevToEig = "+sevToEig+",OSO_EigToNin = "+eigToNin+"," +
			      "OSO_NinToHun = "+ninToHun+" where  OSO_VclID = "+VehicleID+" and OSO_Date = '"+this.workTime+"';";
		}
		return sql;
	}
	/**
	 * 查询PGN参数91 Accelerator Pedal Position 1  加速踏板位置1(%)
	 **/
	private List<Map<String, Object>> queryPgnOfPedalPosition(Map<String,Object> param) {
		
		if(StringUtil.isValid(this.VclIDS)&&StringUtil.isValid(this.workTime)){
			StringBuffer strBuf = new StringBuffer();
			if(null != param){
				String linkDataBase = "openrowset('SQLOLEDB','"+param.get("dataBaseIp")+"';'"+param.get("user")+"';'"+param.get("password")+"',"+param.get("MsgPDataBase")+".dbo.Msg_Pgn_"+workTime.replaceAll("-", "")+")";
				strBuf.append("select MsgP_PGNValue6 as MsgPC_PGNValue,MsgP_Vcl_ID from "+linkDataBase+" mp ");
				strBuf.append("where mp.MsgP_SysPIP_ID6=1 and mp.MsgP_Vcl_ID in ("+this.VclIDS+")");
			}else{
				strBuf.append("select MsgP_PGNValue6 as MsgPC_PGNValue,MsgP_Vcl_ID from "+GlobalDBVar.getDBFullName_Conn("Msg_Pgn_"+workTime.replaceAll("-", ""), "4", workTime+" 00:00:00")+" mp ");
				strBuf.append("where mp.MsgP_SysPIP_ID6=1 and mp.MsgP_Vcl_ID in ("+this.VclIDS+")");
			}
			//System.out.println("pgn:"+strBuf.toString());
			List<Map<String, Object>> listM = this.mcdsDAO.execQuery(strBuf.toString(), new Object[]{});
			//System.out.println("查询PGN参数91 Accelerator Pedal Position 1  加速踏板位置1(%) 条数："+listM.size());
			return listM;
		}else{
			System.out.println("油门开度----查询PGN参数91 Accelerator Pedal Position 1  加速踏板位置1(%) 条数-----设备号为空："+this.VclIDS+"      或时间为空："+this.workTime);
			return null;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
	}

}
